# 定义棋盘大小
BOARD_ROWS = 9
BOARD_COLS = 7

# 定义动物等级
ANIMAL_RANKS = {
    '鼠': 1,
    '猫': 2,
    '狗': 3,
    '狼': 4,
    '豹': 5,
    '虎': 6,
    '狮': 7,
    '象': 8
}

# 初始化棋盘
def init_board():
    board = [[' ' for _ in range(BOARD_COLS)] for _ in range(BOARD_ROWS)]
    # 红方棋子
    board[0][0] = '红象'
    board[0][2] = '红狮'
    board[0][4] = '红虎'
    board[0][6] = '红豹'
    board[1][1] = '红狼'
    board[1][5] = '红狗'
    board[2][0] = '红猫'
    board[2][6] = '红鼠'
    # 蓝方棋子
    board[8][0] = '蓝鼠'
    board[8][6] = '蓝猫'
    board[7][1] = '蓝狗'
    board[7][5] = '蓝狼'
    board[8][2] = '蓝豹'
    board[8][4] = '蓝虎'
    board[8][6] = '蓝狮'
    board[8][0] = '蓝象'
    return board

# 打印棋盘
def print_board(board):
    print("  0 1 2 3 4 5 6")
    for i in range(BOARD_ROWS):
        row_str = str(i) + " "
        for j in range(BOARD_COLS):
            row_str += board[i][j].ljust(2)
        print(row_str)

# 检查移动是否合法
def is_valid_move(board, from_x, from_y, to_x, to_y):
    # 检查是否在棋盘范围内
    if to_x < 0 or to_x >= BOARD_ROWS or to_y < 0 or to_y >= BOARD_COLS:
        return False
    # 检查起始位置是否有棋子
    if board[from_x][from_y] == ' ':
        return False
    # 检查目标位置是否有己方棋子
    current_player = board[from_x][from_y][0]
    target_piece = board[to_x][to_y]
    if target_piece != ' ' and target_piece[0] == current_player:
        return False
    # 检查移动距离是否为 1 格
    if abs(from_x - to_x) + abs(from_y - to_y) != 1:
        return False
    # 检查吃子规则
    if target_piece != ' ':
        attacker = board[from_x][from_y][1:]
        defender = target_piece[1:]
        if attacker == '鼠' and defender == '象':
            return True
        if defender == '鼠' and attacker == '象':
            return False
        return ANIMAL_RANKS[attacker] >= ANIMAL_RANKS[defender]
    return True

# 移动棋子
def move_piece(board, from_x, from_y, to_x, to_y):
    board[to_x][to_y] = board[from_x][from_y]
    board[from_x][from_y] = ' '
    return board

# 主游戏循环
def play_game():
    board = init_board()
    current_player = '红'
    while True:
        print_board(board)
        print(f"当前玩家: {current_player}")
        try:
            from_x, from_y = map(int, input("请输入要移动的棋子的坐标 (x y): ").split())
            to_x, to_y = map(int, input("请输入目标位置的坐标 (x y): ").split())
            if is_valid_move(board, from_x, from_y, to_x, to_y):
                board = move_piece(board, from_x, from_y, to_x, to_y)
                # 切换玩家
                current_player = '蓝' if current_player == '红' else '红'
            else:
                print("无效的移动，请重新输入。")
        except ValueError:
            print("输入无效，请输入两个整数。")

if __name__ == "__main__":
    play_game()